package com.toycloud.system.service.impl.business;

import java.util.*;
import java.util.stream.Collectors;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.toycloud.business.dto.params.business.AssociationApplicationParams;
import com.toycloud.business.dto.params.business.FeedbackApplicationVersionManagerParams;
import com.toycloud.business.dto.params.business.FeedbackProductVersionManagerParams;
import com.toycloud.business.dto.results.CommonResult;
import com.toycloud.common.core.utils.DateUtils;
import com.toycloud.common.core.utils.StringUtils;
import com.toycloud.common.core.utils.uuid.IdUtils;
import com.toycloud.system.domain.business.*;
import com.toycloud.system.mapper.business.FeedbackApplicationVersionManagerMapper;
import com.toycloud.system.mapper.business.FeedbackProductVersionManagerDetailMapper;
import com.toycloud.system.mapper.business.FeedbackProductVersionManagerMapper;
import com.toycloud.system.service.business.IFeedbackProductVersionManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 产品版本管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-18
 */
@Service
public class FeedbackProductVersionManagerServiceImpl implements IFeedbackProductVersionManagerService
{
    @Resource
    private FeedbackProductVersionManagerMapper feedbackProductVersionManagerMapper;
    @Resource
    private FeedbackApplicationVersionManagerMapper feedbackApplicationVersionManagerMapper;
    @Resource
    private FeedbackProductVersionManagerDetailMapper feedbackProductVersionManagerDetailMapper;

    /**
     * 查询产品版本管理
     * 
     * @param id 产品版本管理主键
     * @return 产品版本管理
     */
    @Override

    public FeedbackProductVersionManager selectFeedbackProductVersionManagerById(String id)
    {
        FeedbackProductVersionManager feedbackProductVersionManager = feedbackProductVersionManagerMapper.selectFeedbackProductVersionManagerById(id);
        List<AssociationApplicationParams> applicationsByParentId = feedbackProductVersionManagerDetailMapper.getApplicationsByParentId(id);
        List<OptionVO> list =feedbackApplicationVersionManagerMapper.selectApplicationOption();
        applicationsByParentId.stream().forEach(item->{
            item.setApplicationname(list.stream().filter(item1->item1.getId().equals(item.getApplicationid())).findFirst().get().getName());
        });
        feedbackProductVersionManager.setAssociationApplicationParams(applicationsByParentId);
        return feedbackProductVersionManager;
    }

    /**
     * 查询产品版本管理列表
     * 
     * @param feedbackProductVersionManagerParams 产品版本管理
     * @return 产品版本管理
     */
    @Override
    public CommonResult<List<FeedbackProductVersionManager>> selectFeedbackProductVersionManagerList(FeedbackProductVersionManagerParams feedbackProductVersionManagerParams)
    {
        PageHelper.startPage(feedbackProductVersionManagerParams.getCurrent(), feedbackProductVersionManagerParams.getPageSize());
        if(StringUtils.isNotEmpty(feedbackProductVersionManagerParams.getProducttypeid())){
            feedbackProductVersionManagerParams.setProducttypeids(Arrays.stream(feedbackProductVersionManagerParams.getProducttypeid().split(",")).collect(Collectors.toList()));
        }
        if(StringUtils.isNotEmpty(feedbackProductVersionManagerParams.getProductmodelid())){
            feedbackProductVersionManagerParams.setProductmodelids(Arrays.stream(feedbackProductVersionManagerParams.getProductmodelid().split(",")).collect(Collectors.toList()));
        }
        List<FeedbackProductVersionManager> feedbackProductVersionManagers = feedbackProductVersionManagerMapper.selectFeedbackProductVersionManagerList(feedbackProductVersionManagerParams);
        List<OptionVO> optionVOS = feedbackProductVersionManagerMapper.selectProductTypeOption();
        List<OptionVO> optionVOS1 = feedbackProductVersionManagerMapper.selectProductModelOption();
        feedbackProductVersionManagers.stream().forEach(item->{
            item.setProducttypename(optionVOS.stream().filter(item1->item1.getId().equals(item.getProducttypeid())).findFirst().get().getName());
            item.setProductmodelname(optionVOS1.stream().filter(item1->item1.getId().equals(item.getProductmodelid())).findFirst().get().getName());
        });
        Page<FeedbackProductVersionManager> feedbackProductVersionManagersPage = (Page<FeedbackProductVersionManager>) feedbackProductVersionManagers;
        return CommonResult.Success((int) feedbackProductVersionManagersPage.getTotal(), feedbackProductVersionManagers);
    }
    @Override
    public CommonResult<List<FeedbackProductVersionManager>> export(FeedbackProductVersionManagerParams feedbackProductVersionManagerParams)
    {
        if(StringUtils.isNotEmpty(feedbackProductVersionManagerParams.getProducttypeid())){
            feedbackProductVersionManagerParams.setProducttypeids(Arrays.stream(feedbackProductVersionManagerParams.getProducttypeid().split(",")).collect(Collectors.toList()));
        }
        if(StringUtils.isNotEmpty(feedbackProductVersionManagerParams.getProductmodelid())){
            feedbackProductVersionManagerParams.setProductmodelids(Arrays.stream(feedbackProductVersionManagerParams.getProductmodelid().split(",")).collect(Collectors.toList()));
        }
        List<FeedbackProductVersionManager> feedbackProductVersionManagers = feedbackProductVersionManagerMapper.selectFeedbackProductVersionManagerList(feedbackProductVersionManagerParams);
        List<OptionVO> optionVOS = feedbackProductVersionManagerMapper.selectProductTypeOption();
        List<OptionVO> optionVOS1 = feedbackProductVersionManagerMapper.selectProductModelOption();
        feedbackProductVersionManagers.stream().forEach(item->{
            item.setProducttypename(optionVOS.stream().filter(item1->item1.getId().equals(item.getProducttypeid())).findFirst().get().getName());
            item.setProductmodelname(optionVOS1.stream().filter(item1->item1.getId().equals(item.getProductmodelid())).findFirst().get().getName());
        });
        return CommonResult.Success(feedbackProductVersionManagers);
    }

    /**
     * 新增产品版本管理
     * 
     * @param feedbackProductVersionManager 产品版本管理
     * @return 结果
     */
    @Override
    public CommonResult insertFeedbackProductVersionManager(FeedbackProductVersionManager feedbackProductVersionManager)
    {
        feedbackProductVersionManager.setCreateTime(DateUtils.getNowDate());
        String id = IdUtils.simpleUUID();
        feedbackProductVersionManager.setId(id);
        feedbackProductVersionManagerMapper.insertFeedbackProductVersionManager(feedbackProductVersionManager);
        List<OptionVO> optionVOS = feedbackProductVersionManagerMapper.selectProductTypeOption();
        List<OptionVO> optionVOS1 = feedbackProductVersionManagerMapper.selectProductModelOption();
        String productversion = optionVOS.stream().filter(item1->item1.getId().equals(feedbackProductVersionManager.getProducttypeid())).findFirst().get().getName()+"-"+optionVOS1.stream().filter(item1->item1.getId().equals(feedbackProductVersionManager.getProductmodelid())).findFirst().get().getName()+"-"+feedbackProductVersionManager.getModelversion();
        feedbackProductVersionManager.getApplications().forEach(item->{
            FeedbackProductVersionManagerDetail item1 = new FeedbackProductVersionManagerDetail();
            item1.setParentid(id);
            item1.setApplicationid(item);
            item1.setCreateTime(DateUtils.getNowDate());
            item1.setProductversion(productversion);
            feedbackProductVersionManagerDetailMapper.insertFeedbackProductVersionManagerDetail(item1);
        });
        return CommonResult.Success();
    }

    /**
     * 修改产品版本管理
     * 
     * @param feedbackProductVersionManager 产品版本管理
     * @return 结果
     */
    @Override
    public CommonResult updateFeedbackProductVersionManager(FeedbackProductVersionManager feedbackProductVersionManager)
    {
        feedbackProductVersionManager.setUpdateTime(DateUtils.getNowDate());
        feedbackProductVersionManagerMapper.updateFeedbackProductVersionManager(feedbackProductVersionManager);
        List<OptionVO> optionVOS = feedbackProductVersionManagerMapper.selectProductTypeOption();
        List<OptionVO> optionVOS1 = feedbackProductVersionManagerMapper.selectProductModelOption();
        String productversion = optionVOS.stream().filter(item1->item1.getId().equals(feedbackProductVersionManager.getProducttypeid())).findFirst().get().getName()+"-"+optionVOS1.stream().filter(item1->item1.getId().equals(feedbackProductVersionManager.getProductmodelid())).findFirst().get().getName()+"-"+feedbackProductVersionManager.getModelversion();
        List<AssociationApplicationParams> applicationsByParentId = feedbackProductVersionManagerDetailMapper.getApplicationsByParentId(feedbackProductVersionManager.getId());
        Map map = processLists(applicationsByParentId.stream().map(item -> item.getId()).collect(Collectors.toList()), feedbackProductVersionManager.getApplications());
        List<String> addList = (List<String>) map.get("insert");
        List<String> updateList = (List<String>) map.get("update");
        List<String> deleteList = (List<String>) map.get("delete");
        addList.forEach(item->{
            FeedbackProductVersionManagerDetail item1 = new FeedbackProductVersionManagerDetail();
            item1.setParentid(feedbackProductVersionManager.getId());
            item1.setApplicationid(item);
            item1.setCreateTime(DateUtils.getNowDate());
            feedbackProductVersionManagerDetailMapper.insertFeedbackProductVersionManagerDetail(item1);
        });
        deleteList.forEach(item->{
            feedbackProductVersionManagerDetailMapper.deleteFeedbackProductVersionManagerDetailByApplicationId(item);
        });
        updateList.forEach(item->{
            FeedbackProductVersionManagerDetail item1 = new FeedbackProductVersionManagerDetail();
            item1.setParentid(feedbackProductVersionManager.getId());
            item1.setApplicationid(item);
            item1.setProductversion(productversion);
            feedbackProductVersionManagerDetailMapper.updateByTwo(item1);
        });
        return CommonResult.Success();
    }
    public static Map processLists(List<String> oldList, List<String> newList) {
        // 将列表转换为集合以便快速查找和操作
        Set<String> setOld = new HashSet<>(oldList);
        Set<String> setNew = new HashSet<>(newList);

        // 需要删除的项（旧数据有但新数据没有）
        List<String> toDelete = new ArrayList<>(setOld);
        toDelete.removeAll(setNew);

        // 新增的项（新数据有但旧数据没有）
        List<String> toAdd = new ArrayList<>(setNew);
        toAdd.removeAll(setOld);

        // 更新的项（交集部分）
        List<String> toUpdate = new ArrayList<>(setOld);
        toUpdate.retainAll(setNew); // 保留共同元素

        // 构建返回的结果Map
        Map<String, List<String>> resultMap = new HashMap<>();
        resultMap.put("delete", toDelete);
        resultMap.put("update", toUpdate);
        resultMap.put("insert", toAdd);

        return resultMap;
    }

    /**
     * 批量删除产品版本管理
     * 
     * @param ids 需要删除的产品版本管理主键
     * @return 结果
     */
    @Override
    public int deleteFeedbackProductVersionManagerByIds(Long[] ids)
    {
        return feedbackProductVersionManagerMapper.deleteFeedbackProductVersionManagerByIds(ids);
    }

    /**
     * 删除产品版本管理信息
     * 
     * @param id 产品版本管理主键
     * @return 结果
     */
    @Override
    public int deleteFeedbackProductVersionManagerById(String id)
    {
        return feedbackProductVersionManagerMapper.deleteFeedbackProductVersionManagerById(id);
    }

    @Override
    public List<OptionVO> selectProductBrandOption() {
        return feedbackProductVersionManagerMapper.selectProductBrandOption();
    }

    @Override
    public List<OptionVO> selectProductTypeOption() {
        return feedbackProductVersionManagerMapper.selectProductTypeOption();
    }

    @Override
    public List<OptionVO> selectProductModelOption(String id,String type) {
        return feedbackProductVersionManagerMapper.selectProductModelOption(id,type);
    }
    @Override
    public List<OptionVO> selectThreeParamOption(String id ) {
        return feedbackProductVersionManagerMapper.selectThreeParamOption(id);
    }
}
